package com.kehao.accountbook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.kehao.accountbook.pojo.Expenditure;
import com.kehao.accountbook.pojo.SimpleDate;
import com.kehao.accountbook.pojo.SimpleTime;

import java.util.ArrayList;
import java.util.List;

public class AddExpendActivity extends AppCompatActivity implements View.OnClickListener{
    boolean flag = true;
    int type = Expenditure.TYPE_NORMAL;
    private Button btnPay;
    private Button btnGet;
    private EditText et_money;
    private EditText et_comment;

    List<ImageView> imageViewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_expend);
        init();
    }

    private void init() {
        btnPay = findViewById(R.id.btn_pay);
        btnGet = findViewById(R.id.btn_get);
        et_money = findViewById(R.id.et_money);
        et_comment = findViewById(R.id.et_comment);
        imageViewList = new ArrayList<>();
        imageViewList.add((ImageView) findViewById(R.id.img_normal));
        imageViewList.add((ImageView) findViewById(R.id.img_shopping));
        imageViewList.add((ImageView) findViewById(R.id.img_eatting));
        imageViewList.add((ImageView) findViewById(R.id.img_financial));
        for(ImageView i :imageViewList){
            i.setOnClickListener(AddExpendActivity.this);
        }
    }

    public void onClickSubmit(View view) {
        Intent intent = new Intent();
        String money_str = et_money.getText().toString();
        double money;
        String comment = et_comment.getText().toString();
        if(!money_str.equals("")){
            if(flag){
                money = Double.parseDouble("-"+money_str);
            }else {
                money = Double.parseDouble(money_str);
            }
            Expenditure expenditure =
                    new Expenditure(((AccountApplication)getApplication()).getUsername(),new SimpleDate(), new SimpleTime(),type,money,comment);
            intent.putExtra("item",expenditure);
            setResult(RESULT_OK,intent);
        }else {
            setResult(RESULT_CANCELED,intent);
        }
        finish();
    }

    public void onClickPay(View view) {
        flag = true;
        btnPay.setBackgroundColor(Color.parseColor("#FFFFFF"));
        btnGet.setBackgroundColor(Color.parseColor("#EEEEEE"));
    }

    public void onClickGet(View view) {
        flag = false;
        btnPay.setBackgroundColor(Color.parseColor("#EEEEEE"));
        btnGet.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }

    @Override
    public void onClick(View v) {
        for(ImageView i :imageViewList){
            i.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }
        v.setBackgroundColor(Color.parseColor("#F44336"));
        switch (v.getId()){
            case R.id.img_shopping:
                type = Expenditure.TYPE_SHOPPING;
                break;
            case R.id.img_normal:
                type = Expenditure.TYPE_NORMAL;
                break;
            case R.id.img_financial:
                type=Expenditure.TYPE_FINANCIAL;
                break;
            case R.id.img_eatting:
                type=Expenditure.TYPE_EATTING;
                break;
        }
    }
}
